package com.haoxin.ai_course.modules.course.controller.admin;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.haoxin.ai_course.common.annotation.SysLog;
import com.haoxin.ai_course.common.base.AbstractController;
import com.haoxin.ai_course.common.utils.DateUtil;
import com.haoxin.ai_course.common.utils.EasyExcelUtil;
import com.haoxin.ai_course.common.utils.R;
import com.haoxin.ai_course.modules.course.entity.AcStatisticsAnswer;
import com.haoxin.ai_course.modules.course.entity.VO.AcStatisticsAnswerExcelVO;
import com.haoxin.ai_course.modules.course.service.AcStatisticsAnswerService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import ma.glasnost.orika.MapperFacade;



/**
 * 题目统计表表
 * 
 * @author xingxiangguo
 * @email bugcn@qq.com
 * @date 2022-06-06 09:44:49
 */
@RestController
@AllArgsConstructor
@RequestMapping("/course/acstatisticsanswer")
@Api(value="api",tags="后台-答题项统计管理")
public class AcStatisticsAnswerController  extends AbstractController {
    private final  AcStatisticsAnswerService acStatisticsAnswerService;
    private final  MapperFacade mapperFacade;
    /**
     * 列表
     */
    @RequestMapping("/list")
    @ApiOperation("题目统计计列表")
    @RequiresPermissions("course:acstatisticsanswer:list")
    public R list(@RequestParam Map<String, Object> params){
        //查询列表数据
        QueryWrapper<AcStatisticsAnswer> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(AcStatisticsAnswer::getCourseId, params.get("courseId"));
        queryWrapper.lambda().orderByDesc(AcStatisticsAnswer::getCourseId).orderByDesc(AcStatisticsAnswer::getTopicCode);
        IPage<AcStatisticsAnswer> sysConfigList = acStatisticsAnswerService.page(mpPageConvert.<AcStatisticsAnswer>pageParamConvert(params),queryWrapper);
        return R.ok().put("page",mpPageConvert.pageValueConvert(sysConfigList));
    }
    /**
     * 列表
     */
    @RequestMapping("/listV2")
    @ApiOperation("题目统计计列表V2")
    @RequiresPermissions("course:acstatisticsanswer:list")
    public R listV2(@RequestParam Map<String, Object> params){
    	//查询列表数据
    	QueryWrapper<AcStatisticsAnswer> queryWrapper = new QueryWrapper<>();
    	queryWrapper.lambda().eq(AcStatisticsAnswer::getCourseId, params.get("courseId"));
    	queryWrapper.lambda().groupBy(AcStatisticsAnswer::getTopicCode);
    	queryWrapper.lambda().orderByAsc(AcStatisticsAnswer::getTopicCode);
    	IPage<AcStatisticsAnswer> sysConfigList = acStatisticsAnswerService.page(mpPageConvert.<AcStatisticsAnswer>pageParamConvert(params),queryWrapper);
    	List<AcStatisticsAnswer> acStatisticsAnswer = sysConfigList.getRecords();
    	for (AcStatisticsAnswer acStatisticsAnswer2 : acStatisticsAnswer) {
    		QueryWrapper<AcStatisticsAnswer> queryWrapper1 = new QueryWrapper<>();
    		queryWrapper1.lambda().eq(AcStatisticsAnswer::getTopicCode, acStatisticsAnswer2.getTopicCode());
    		queryWrapper1.lambda().orderByAsc(AcStatisticsAnswer::getAnswerId);
    		String answerDetail = "";
    		List<AcStatisticsAnswer> acStatisticsAnswer1 =  acStatisticsAnswerService.list(queryWrapper1);
    		for (AcStatisticsAnswer acStatisticsAnswer3 : acStatisticsAnswer1) {
    			answerDetail += "选项:"+acStatisticsAnswer3.getAnswerCode()+" 答案内容："+acStatisticsAnswer3.getAnswerContent()+" 选中人数: "+acStatisticsAnswer3.getSelectedCount()+" 选中人数占比:"+acStatisticsAnswer3.getSelectedRate()+"\n";	
			}
    		acStatisticsAnswer2.setAnswerDetail(answerDetail);
		}
    	return R.ok().put("page",mpPageConvert.pageValueConvert(sysConfigList));
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("course:acstatisticsanswer:info")
    public R info(@PathVariable("id") Integer id){
        return R.ok().put("acStatisticsAnswer", acStatisticsAnswerService.getById(id));
    }


    /**
     * 新增题目统计表表
     */
    @SysLog("新增题目统计表表数据")
    @RequestMapping("/save")
    @RequiresPermissions("course:acstatisticsanswer:save")
    public R save(@RequestBody AcStatisticsAnswer acStatisticsAnswer){
        acStatisticsAnswerService.save(acStatisticsAnswer);
        return R.ok();
    }


    /**
     * 修改
     */
    @SysLog("修改题目统计表表数据")
    @RequestMapping("/update")
    @RequiresPermissions("course:acstatisticsanswer:update")
    public R update(@RequestBody AcStatisticsAnswer acStatisticsAnswer){
		acStatisticsAnswerService.updateById(acStatisticsAnswer);
        return R.ok();
    }


    /**
     * 删除
     */
    @SysLog("删除题目统计表表数据")
    @RequestMapping("/delete")
    @RequiresPermissions("course:acstatisticsanswer:delete")
    public R delete(@RequestBody Integer[] ids){
		acStatisticsAnswerService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }
    @GetMapping("exportExcel")
    @ApiOperation("导出excel")
	public void exportExcel(HttpServletResponse response) {
		List<AcStatisticsAnswer> acStatisticsAnswerList = acStatisticsAnswerService.list();
		List<AcStatisticsAnswerExcelVO> acStatisticsAnswerVOList=mapperFacade.mapAsList(acStatisticsAnswerList, AcStatisticsAnswerExcelVO.class);
		try {
			EasyExcelUtil.exprotExcel(response, acStatisticsAnswerVOList, "课程题目统计表-" +DateUtil.localDateTimeFormat(LocalDateTime.now(), "yyyyMMdd"),
			"题目统计表" ,
			AcStatisticsAnswerExcelVO.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
